<template>
	<view>
		<view class="container">
			<view class="fuel-code-number" @click="!countFlag?refresh():'ope'">
				<text>{{ fuelCode }}</text>
				<view class="left"></view>
				<view class="right"></view>
				<view class="refresh">
					<image :class="isRotate ? 'rotate' : 'rotate1'" src="http://wx.xiongmaovip.com/miniapp/static/personal-center/refresh.png" mode=""></image>
					<text>{{btnMsg == null ? countNum+'s' : btnMsg}}</text>
				</view>
			</view>
			<view class="use-note">
				<view class="note-title">加油码使用说明</view>
				<view class="note-content">
					1.该加油码有效期为1分钟，过期自动刷新。
					<br />
					2.请将加油码告知加油员，或者在加油机键盘上按小数点"."后输入根据提示输入加油码自助加油。
					<br />
					3.使用加油码加油将直接从会员账户中扣减相应的余额。
					<br />
				</view>
			</view>
		</view>
		<custom-tabbar :currPage="1" :current-type="1" />
	</view>

</template>

<script>
	import {
		fuelCode
	} from '@/api/index.js';
	import {
		toast
	} from '../../../utils/common';
	import customTabbar from '@/components/custom-tabbar.vue'
	export default {
		components: {
			customTabbar
		},
		data() {
			return {
				isShow: false,
				fuelCode: '获取中...',
				refreshCode: null,
				isRotate: false,
				// 倒计时周期
				countNum: 10,
				// 用于倒计时标记，true-正在倒计时
				countFlag: false,
				// 定时器
				intervalBtn: {},
				//默认按钮的值
				btnMsg: '刷新'
			};
		},
		onLoad() {
			this.refreshCode = setInterval(this.getFuelCode, 65 * 1000);
			this.getFuelCode();
		},
		onUnload() {
			if (this.refreshCode) {
				clearInterval(this.refreshCode);
				this.refreshCode = null;
			}
		},
		methods: {
			getFuelCode() {
				fuelCode().then(res => {
						if (res.code === 200) {
							this.fuelCode = res.msg;
							this.isRotate = false;
						}
					})
					.catch(err => {
						toast(err)
					});
			},
			countDown() {
				// 设置btn倒计时时显示的信息
				this.btnMsg = null;
				// 更改btn状态
				this.countFlag = true;
				// 设置倒计时
				this.intervalBtn = setInterval(() => {
					if (this.countNum <= 0) {
						// 重置btn提示信息
						this.btnMsg = '刷新';
						// 清除定时器
						clearInterval(this.intervalBtn);
						// 更改btn状态
						this.countFlag = false;
						// 重置倒计时状态
						this.countNum = 10;
					}
					// 倒计时
					this.countNum--;
				}, 1000);
			},
			//刷新加油码
			refresh() {
				this.isRotate = true;
				this.getFuelCode();
				this.countDown()
			}
		}
	};
</script>

<style lang="scss" scoped>
	.container {
		//background: linear-gradient(49deg, #5a97ff 0%, #1a69f1 100%) !important;
    background: #006241;
		position: fixed;
		width: 100%;
		padding: 85rpx 24rpx 0rpx 24rpx;
		min-height: 100%;
		//background: #ff4d4f;
		box-sizing: border-box;

		.fuel-code-title {
			font-size: 40rpx;
			font-weight: bold;
			color: white;
			text-align: center;
			margin: 40rpx;
		}

		.fuel-code-number {
			padding: 72rpx 90rpx 60rpx 90rpx;
			text-align: center;
			font-size: 80rpx;
			font-weight: bold;
			letter-spacing: 30rpx;
			// border-radius: 20rpx;
			border-bottom-left-radius: 20rpx;
			border-bottom-right-radius: 20rpx;
			background: #ffffff;
			color: #333333;
			position: relative;

			.left {
				position: absolute;
				width: 35%;
				height: 40rpx;
				left: 0;
				top: -24rpx;
				background: linear-gradient(-135deg, transparent 10px, #ffffff 0);
				border-top-right-radius: 140rpx;
				border-top-left-radius: 80rpx;
			}

			.right {
				position: absolute;
				width: 35%;
				height: 40rpx;
				right: 0;
				top: -24rpx;
				background: linear-gradient(135deg, transparent 10px, #ffffff 0);
				border-top-left-radius: 140rpx;
				border-top-right-radius: 80rpx;
			}

			.refresh {
				position: absolute;
				top: 0;
				right: 30rpx;
				display: flex;
				align-items: center;
				justify-content: center;

				image {
					width: 32rpx;
					height: 32rpx;
					color: #c0c0c0;
				}

				text {
					font-size: 24rpx;
					letter-spacing: 0rpx;
					margin-left: 10rpx;
					color: #c0c0c0;
				}

				.rotate {
					animation: mymove 1s infinite;
					-webkit-animation: mymove 1s infinite;
				}

				@keyframes mymove {
					from {
						transform: rotate(0deg);
					}

					to {
						transform: rotate(360deg);
					}
				}
			}
		}

		.use-note {
			background: #ffffff;
			border-radius: 16rpx;
			margin: 50rpx auto;
			line-height: 180%;
			padding: 34rpx 16rpx 34rpx 24rpx;

			.note-title {
				font-weight: bold;
				font-size: 34rpx;
				color: #333333;
				line-height: 34rpx;
				margin-bottom: 16rpx;
			}

			.note-content {
				font-size: 30rpx;
				color: #999999;
				line-height: 60rpx;
			}
		}
	}
</style>